package com.example.demo.item.custom;

import com.example.demo.util.ModTags;
import net.minecraft.block.BlockState;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class Prospector extends Item {
    public Prospector(Settings settings) {
        super(settings);
    }

    @Override
    public ActionResult useOnBlock(ItemUsageContext context) {
        World world = context.getWorld();
        if (!world.isClient()) {
            BlockPos pos = context.getBlockPos();
            PlayerEntity player = context.getPlayer();
            boolean found = false;
            for (int i = -64; i <= pos.getY() + 64; i++) {
                BlockState blockState = world.getBlockState(pos.down(i));
                if (blockState.isIn(ModTags.Blocks.PROSPECTOR_LIST)) {
                    //查找到矿后给玩家发送矿物的名称和坐标
                    if (player != null) {
                        player.sendMessage(Text.literal("Find " + blockState.getBlock().asItem().getName().getString() + " at " + pos.getX() + " " + pos.getY() + " " + pos.getZ()));
                    }
                    found = true;
                    break;
                }
            }
            if (!found) {
                if (player != null) {
                    player.sendMessage(Text.literal("No ore found"));
                }
            }
        }
        context.getStack().damage(1, context.getPlayer(), (player) -> {
            player.sendToolBreakStatus(player.getActiveHand());
        });
        return ActionResult.SUCCESS;
    }

    /**
     * 重写此方法以向物品堆添加额外的工具提示信息
     * 它允许在物品的工具提示中包含额外的信息或自定义文本
     *
     * @param stack 物品堆对象，包含有关物品的信息
     * @param world 可能是null，如果非空，则是世界对象，可用于获取世界特定数据
     * @param tooltip 工具提示文本的列表，可以通过此参数添加自定义的工具提示行
     * @param context 工具提示的上下文，提供有关如何显示工具提示的信息
     */
    @Override
    public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
          tooltip.add(Text.translatable("item.demo.prospector.tooltip"));
        super.appendTooltip(stack, world, tooltip, context);
    }

}
